using System;
using System.IO;
using UnityEngine;
using Newtonsoft.Json;

public class DataManager : MonoBehaviour
{
    public static DataManager Instance { get; private set; }

    public float MusicSettingValue;
    public float SFXSettingValue;

    void Awake()
    {
        if (Instance!=null)
        {
            Destroy(gameObject);
            return;
        }
        Instance = this;
        DontDestroyOnLoad(gameObject);
    }
    
    [Serializable]
    class SaveData
    {
        public float musicSetting;
        public float sfxSetting;
    }

    public void SaveSettings()
    {
        SaveData data = new SaveData();
        data.musicSetting = MusicSettingValue;
        data.sfxSetting = SFXSettingValue;

        string json = JsonConvert.SerializeObject(data);
        File.WriteAllText(Application.persistentDataPath + "/saveSettings.json", json);
    }
    
    public void LoadSettings()
    {
        string path=Application.persistentDataPath + "/saveSettings.json";
        if (File.Exists(path))
        {
            string json = File.ReadAllText(path);
            SaveData data = JsonConvert.DeserializeObject<SaveData>(json);
            MusicSettingValue = data.musicSetting;
            SFXSettingValue = data.sfxSetting;
        }
        else
        {
            MusicSettingValue = 0.5f;
            SFXSettingValue = 0.5f;
        }
    }
}
